<?php
require_once '../data/connect.class.php';
$connect = new Connect("localhost","root","","email");

$type=$_REQUEST['type'];

$error = "";
$msg = "";
$fileElementName = 'fileToUpload';
$uploadedFile = "";
if(!empty($_FILES[$fileElementName]['error']))
{
	switch($_FILES[$fileElementName]['error'])
	{

		case '1':
			$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
			break;
		case '2':
			$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
			break;
		case '3':
			$error = 'The uploaded file was only partially uploaded';
			break;
		case '4':
			$error = 'No file was uploaded.';
			break;

		case '6':
			$error = 'Missing a temporary folder';
			break;
		case '7':
			$error = 'Failed to write file to disk';
			break;
		case '8':
			$error = 'File upload stopped by extension';
			break;
		case '999':
		default:
			$error = 'No error code avaiable';
	}
}elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none')
{
	$error = 'No file was uploaded..';
}else 
{
	$msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
	$msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);
	$uploadedFile = "../resources/uploads/" . $_FILES["fileToUpload"]["name"];
	move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"../resources/uploads/" . iconv("UTF-8","GB2312//IGNORE",$_FILES["fileToUpload"]["name"]));
		
}		
echo "{";
echo				"error: '" . $error . "',\n";
echo				"msg: '" . $msg . "'\n";
echo "}";

$classId = 3;

if ($type=='bill') {
    $classId = 4;
} elseif ($type=='login') {
    $classId = 5;
} elseif ($type=='all') {
    $classId = 3;
}

$file = fopen($uploadedFile,"rb");
while ($data = fgetcsv($file, 1024, ",")) {
	$userName = '';
	$addr = '';
	$domain = '';
	$subscripted = 1;
	$array = array();
	if (count($data) > 1) {
		$userName = $data[0];
		$addr = $data[1];
		if (isset($data[2])) {
			$subscripted = $data[2];
		}
	} else {
		$addr = $data[0];
	}
	
    $pos = strpos($addr,'@');
	if($pos === false) {
        continue;
    }
    $domain = substr($addr, $pos + 1);
        
    $array['name'] = $userName;
    $array['mail_addr'] = $addr;
    $array['domain'] = $domain;
    $array['class_id'] = $classId;
    $array['subcripted'] = $subscripted;
        
    $connect->insertSql('admin_mail_addr',$array);
}
fclose($file);

?>